<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\GoodsCategory;
use addons\shopro\model\Goods as GoodsModel;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 首页基础数据
     */
    public function info()
    {
        $banners = Banner::getHomeBanner();
        $category = GoodsCategory::getHomeCategory();

        $this->success('', [
            'banners'  => $banners,
            'category' => $category
        ]);
    }

    public function goods()
    {
        $params = $this->request->param();

        $cateName = '';
        if (!empty($params['type'])) {
            if ($params['type'] == 'eye') {
                $cateName = '眼部产品';
            } elseif ($params['type'] == 'face') {
                $cateName = '面部产品';
            }
        }

        if ($cateName) {
            $category = GoodsCategory::where('name', $cateName)->find();
            if ($category) {
                $params['category_id'] = $category['id'];
            }
        } else {
            $this->success('', [
                'total' => 0,
                'per_page' => 10,
                'current_page' => 1,
                'last_page' => 1,
                'data' => []
            ]);
        }

        $goods = GoodsModel::getGoodsList($params);

        $this->success('', $goods);
    }

    /*
     * 首页升级详情
     */
    public function userLevelNeed()
    {
        $user = $this->auth->getUser();
        $data = (new \app\common\logic\UserLevelLogic())->getUserLevelUp(!empty($user) ? $user->id : 0);
        $this->success('首页等级', $data);
    }
}
